package com.etc.service;

import java.util.List;

import javax.annotation.Resource;

import com.etc.dao.EquipmentMapper;
import com.etc.entity.Equipment;
import com.etc.entity.EquipmentExample;
import com.etc.entity.EquipmentKey;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 消耗品业务层
 * @author Administrator
 *
 */
@Service
public class EquipmentService {

	private static final Logger log = LoggerFactory.getLogger(EquipmentService.class);

	@Resource
	private EquipmentMapper equipmentMapper;


	/**
	 * 领取消耗品
	 */
	public Integer geta(String id ,String neumber){
		Integer restle = null;
		EquipmentKey equipmentKey = new EquipmentKey();
		equipmentKey.setId(Integer.valueOf(id));
		Equipment equipment =  equipmentMapper.selectByPrimaryKey(equipmentKey);
		if (equipment != null ){
			EquipmentExample equipmentExample = new EquipmentExample();
			EquipmentExample.Criteria criteria = equipmentExample.createCriteria();
			criteria.andIdEqualTo(equipment.getId());
			equipment.setStatus(String.valueOf(Integer.valueOf(equipment.getStatus())-Integer.valueOf(neumber)));
			restle = equipmentMapper.updateByExample(equipment,equipmentExample);
		}

		return restle;
	}


	/**
	 * 查询所有消耗品
	 * @return
	 */
	public Equipment findById(String id){
		EquipmentKey equipmentKey = new EquipmentKey();
		equipmentKey.setId(Integer.valueOf(id));
		return equipmentMapper.selectByPrimaryKey(equipmentKey);
	}

	/**
	 * 查询所有消耗品
	 * @return
	 */
	public PageInfo findEquipment(Integer page1){
		PageHelper.startPage(page1, 10);
		EquipmentExample equipmentExample = new EquipmentExample();
		equipmentExample.setOrderByClause("id desc");
		PageInfo pageInfo = new PageInfo(equipmentMapper.selectByExample(equipmentExample));
		return pageInfo;
	}

	/**
	 * 根据id查询消耗品
	 * @param key
	 * @return
	 */
	public Equipment findEquipmentById(EquipmentKey key){
		return equipmentMapper.selectByPrimaryKey(key);
	}
	
	/**
	 * 添加消耗品
	 * @param equipment
	 * @return
	 */
	public Integer add(Equipment equipment){
		equipment.setSaveDate(String.valueOf(System.currentTimeMillis()));
		return equipmentMapper.insert(equipment);
	}
	
	/**
	 * 消耗品
	 * @param id
	 * @return
	 */
	public Integer delete(String id){
		Integer reste = null;
		try{
			EquipmentKey equipmentKey = new EquipmentKey();
			equipmentKey.setId(Integer.valueOf(id));
			reste = equipmentMapper.deleteByPrimaryKey(equipmentKey);
		}catch (Exception e) {
		log.error(e.getLocalizedMessage());
		}
		return reste;
	}
	
	/**
	 * 更新消耗品
	 * @param equipment
	 * @return
	 */
	public Integer update(Equipment equipment){
		Integer reste = null;
		try{
	        reste = equipmentMapper.updateByPrimaryKey(equipment);
		}catch (Exception e) {
		log.error(e.getLocalizedMessage());
		}
		return reste;
	}



}
